package com.sykj.servies.impl;

import com.sykj.dao.LoginDao;
import com.sykj.dao.ProductDao;
import com.sykj.pojo.*;
import com.sykj.servies.CategoryServies;
import com.sykj.servies.LoginServies;
import com.sykj.servies.ProductServies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductServiesImpl implements ProductServies{

    @Autowired
    ProductDao productDao;
    //商品分类
    @Autowired
    private CategoryServies categoryService;
    //用户Dao接口
    @Autowired
    private LoginServies loginServies;
    /**
     *
     */
    //用户servies
    @Autowired
    private LoginDao loginDao;

    //查询商品表
    @Override
    public List<Product> list() {
        //获得商品列表
        List<Product> products=productDao.selectByExample(null);
        //通过for循环遍历，涉及的两个主外键
        for (Product p:products){
            //通过主外键关系得到该商品分类的id
            Category category=categoryService.get(p.getCid());
            p.setCategory(category);
            //通过主外键关系得到当前登录用户id
            User user = loginServies.get(p.getBid());
            p.setUser(user);
        }
        return products;
    }

    //商品下线
    @Override
    public String enableStatus(String name) {
        productDao.enableStatus(name);
        return  "success";
    }
    //商品上线
    @Override
    public String stopStatus(String name) {
        productDao.stopStatus(name);
        return "success";
    }

    //获取图片路径
    @Override
    public void setImageURL(ProductVO vo) {
        productDao.setImageUrl(vo);
    }



    //增加
    @Override
    public void save(Product product) {
        product.setStatus(1);
        productDao.insert(product);
    }
    //根据商品删除
    @Override
    public void del(int id) {
        productDao.deleteByPrimaryKey(id);
    }
    //得到单个商品
    @Override
    public Product get(int id) {
        return productDao.selectByPrimaryKey(id);
    }

    //通过商品id 返回所属分类
    @Override
    public List<Product> getProductsByCid(Integer id) {
        ProductExample example = new ProductExample();
        example.createCriteria().andCidEqualTo(id).andStatusEqualTo(1);
        return productDao.selectByExample(example);
    }
    //通过id返回所属商家
    @Override
    public User getUserByBid(long id) {
        return  loginDao.selectByPrimaryKey(id);
    }

    //修改商品
    @Override
    public void update(Product product) {
        productDao.updateByPrimaryKeySelective(product);
    }
    //得到分类id
    @Override
    public Category getCategoryByCid(int id) {
        return categoryService.get(id);
    }


    //查询所有
    @Override
    public List<Product> findByName(String pName) {
        ProductExample example = new ProductExample();
        example.createCriteria().andNameLike("%"+pName+"%");
       // 根据分类姓名查询商品信息
        List<Product> products = productDao.selectByExample(example);
        return products;
    }
    //查询所有
    @Override
    public List<Product> findByCid(int cid) {
        ProductExample example = new ProductExample();
        example.createCriteria().andCidEqualTo(cid);
        //根据分类id查询商品信息
        List<Product> products = productDao.selectByExample(example);
        return products;
    }

    //
    @Override
    public boolean findProByCid(int cid) {
        ProductExample example = new ProductExample();
        example.createCriteria().andCidEqualTo(cid);
        List<Product> products = productDao.selectByExample(example);
        //当该分类商品不为空为true,为空为false
        return products.size()>0&&products!=null?true:false;
    }

}
